var vm_dangpicibofu = new Vue({
    el : "#dangpicibofu",
    data : {
        deptid : $("#deptid").val(),		// 单位ID

        jingfeipicilist : [],//

        //查询用字段
        picimingcheng : '',

        deptlist:[], //发放单位list
        deptids:'', //勾选发放单位ids
        zhuanjialist:[], //发放专家list
        zhuanjiaids:'' //勾选发放专家ids

    },
    created : function(){
        var _this = this;

        _this.bindJingfeiPiciList();

    },
    methods : {

        // 根据单位ID查询下级单位
        bindJingfeiPiciList : function(){
            var _this = this;

            var jingfeipici_layer = layer.open({type:3});
            $.post('/system/jingfeipici/find_custom_list_now', {
                deptid : _this.deptid,
                picimingcheng : _this.picimingcheng,
                rdm : Math.random()
            },function(ppData) {
                layer.close(jingfeipici_layer);

                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.jingfeipicilist = data;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        reset:function () {
            this.picimingcheng=""
            this.bindJingfeiPiciList()
        },

        Export:function(jingfeipiciitem){
            var _this = this;
            location.href = "/export/daochu_bokuanbiao?piciid="+jingfeipiciitem.id
        },

        //
        toJingfeiPiciDetail : function(jingfeipiciitem){
            $.modal.openTab("单位拨款详情",'system/jingfeipici/jingfei_dept?jingfeipiciid='+jingfeipiciitem.id+"&deptid="+jingfeipiciitem.deptid,false);
        },

        //
        toDanweiqueren : function(jingfeipiciitem){
            $.modal.openTab("单位确认情况",'system/jingfeipici/jingfei_danweiqueren?jingfeipiciid='+jingfeipiciitem.id+"&deptid="+jingfeipiciitem.deptid,false);
        },

        toJisuan : function(jingfeipiciitem){
            var _this = this;
            var jingfeipici_layer = layer.open({type: 3});
            $.post('/system/jingfeipici/jisuan', {
                id: jingfeipiciitem.id,
                deptid : _this.deptid,
                zhuanjiazhongxinpiciid : jingfeipiciitem.zhuanjiazhongxinpiciid,
                jisuanshijian : jingfeipiciitem.jisuanshijian,
                rdm: Math.random()
            }, function (ppData) {
                layer.close(jingfeipici_layer);
                if (ppData != null) {

                    if (ppData.result == "1") {

                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "计算成功!",
                        });

                        _this.bindJingfeiPiciList();

                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        // 发放查询单位列表
        bindDeptList : function(jingfeipiciId){
            var _this = this;

            $.post('/system/jingfeidept/fafanglist', {
                deptId : _this.deptid,
                jingfeipiciId:jingfeipiciId,
                rdm : Math.random()
            },function(ppData) {
                if (ppData != null) {
                    if (ppData.result==1){
                        var data = ppData.resultContent;
                        _this.deptlist = data.deptList;
                        _this.zhuanjialist = data.zhuanjiaList;
                    }else{
                        layer.alert(ppData.message);
                    }
                }
            },"json");
        },

        //发放经费
        toFafangJingfei: function (jingfeipiciitem) {
            var _this = this
            _this.jingfeipiciid = jingfeipiciitem.id
            _this.bindDeptList(jingfeipiciitem.id)
            $("#fafangLabel").html("发放");
            $("#fafangmodal-dialog").attr("style","width:1500px;");
            $("#fafangModal").modal();
        },

        //发放经费
        FafangJingfei: function () {
            var _this = this;
            var chk_value_dept =[];
            var chk_value_zhuanjia =[];

            _this.deptids = '';
            _this.zhuanjiaids = '';

            $('input[name="dept"]:checked').each(function(){
                chk_value_dept.push($(this).val());
            });
            $('input[name="zhuanjia"]:checked').each(function(){
                chk_value_zhuanjia.push($(this).val());
            });
            if (chk_value_dept.length==0&&chk_value_zhuanjia.length==0){
                layer.alert('您还没有选择任何单位或专家！');
                return
            }else {
                for (var i = 0; i < chk_value_dept.length; i++) {
                    for (var j = 0; j < _this.deptlist.length; j++) {
                        if (chk_value_dept[i] == _this.deptlist[j].deptId) {
                            _this.deptids = _this.deptids + _this.deptlist[j].deptId + ",";
                        }
                    }
                }
                for (var i = 0; i < chk_value_zhuanjia.length; i++) {
                    for (var j = 0; j < _this.zhuanjialist.length; j++) {
                        if (chk_value_zhuanjia[i] == _this.zhuanjialist[j].zhuanjiaId) {
                            _this.zhuanjiaids = _this.zhuanjiaids + _this.zhuanjialist[j].zhuanjiaId + ",";
                        }
                    }
                }
            }
            $.post('/system/jingfeidept/fafang', {
                deptIdString: _this.deptids,
                zhuanjiaIdString: _this.zhuanjiaids,
                jingfeipiciId: _this.jingfeipiciid
            }, function (ppData) {
                if (ppData != null) {
                    if (ppData.result == "1") {
                        layer.open({
                            time: 1000,
                            btn: [],
                            content: "发放成功!",
                        })
                        _this.bindJingfeiPiciList()
                        $("#fafangModal").modal('hide');
                    } else {
                        layer.alert(ppData.message);
                    }
                }
            }, "json");
        },

        //
        toJingfeiPiciDetail : function(jingfeipiciitem){
            $.modal.openTab("单位拨款详情",'system/jingfeipici/jingfei_dept?jingfeipiciid='+jingfeipiciitem.id+"&deptid="+jingfeipiciitem.deptid,false);
        },

        //全选按钮的点击事件
        checkAll1(){
            //获取id的dom元素
            let Id = document.getElementById('allAndNotAll1');
            //获取类名为chBox的dom元素
            let chBox = document.getElementsByClassName('dept');
            let len = chBox.length;
            //输出观察

            for(let i=0;i<len;i++){
                //当全选按钮为true时，全部按钮都为true，否则相反
                if(Id.checked){
                    chBox[i].checked=true;
                }
                else {
                    chBox[i].checked=false;
                }
            }
        },
        //单个按钮的点击事件
        checkOne1(){
            let count=0;
            let chBox = document.getElementsByClassName('dept');
            let len = chBox.length;
            for(let i=0;i<len;i++){
                //记录单选按钮为true的数量
                if(chBox[i].checked){
                    count++;
                }
            }
            //当单选按钮都为true时，全选按钮也为true，否则为false
            if(count==len){
                document.getElementById('allAndNotAll1').checked = true;
            }else {
                document.getElementById('allAndNotAll1').checked = false;
            }
        },

        //全选按钮的点击事件
        checkAll2(){
            //获取id的dom元素
            let Id = document.getElementById('allAndNotAll2');
            //获取类名为chBox的dom元素
            let chBox = document.getElementsByClassName('zhuanjia');
            let len = chBox.length;
            //输出观察

            for(let i=0;i<len;i++){
                //当全选按钮为true时，全部按钮都为true，否则相反
                if(Id.checked){
                    chBox[i].checked=true;
                }
                else {
                    chBox[i].checked=false;
                }
            }
        },
        //单个按钮的点击事件
        checkOne2(){
            let count=0;
            let chBox = document.getElementsByClassName('zhuanjia');
            let len = chBox.length;
            for(let i=0;i<len;i++){
                //记录单选按钮为true的数量
                if(chBox[i].checked){
                    count++;
                }
            }
            //当单选按钮都为true时，全选按钮也为true，否则为false
            if(count==len){
                document.getElementById('allAndNotAll2').checked = true;
            }else {
                document.getElementById('allAndNotAll2').checked = false;
            }
        },

    },
})